package com.lagou.tomcat;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

import javax.servlet.ServletException;

public class SpringApplication {

    public static void run(){
        System.out.println("初始化 Tomcat......");
        // 创建Tomcat实例
        Tomcat tomcat = new Tomcat();
        // 设置Tomcat端口
        tomcat.setPort(8081);

        try {
            // 设置项目文件路径
            tomcat.addWebapp("/", "D:\\");
            // 启动Tomcat
            tomcat.start();
            // 异步进行接收请求，阻塞式。没有这一句，方法执行完会直接结束
            tomcat.getServer().await();

        } catch (ServletException e) {
            e.printStackTrace();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }

    }
}
